package com.jbacon.everadiodownloader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jbacon.everadiodownloader.utils.CLICommand;
import com.jbacon.everadiodownloader.utils.RegexMatcher;

public class EveRadioDownloader {

    private static final String PROGRAM_CURL = "curl ";
    private static final String CONFIG_CURL = "-s ";
    private static final String CONFIG_CURL_DEBUG = "-r 0-499 ";
    private static final String URL_SUBSPACE_RADIO = "http://subspace-radio.net/radio/rewind";
    private static final String URL_EVE_RADIO = "http://eve-radio.com/radio/rewind/?noplayer=true";
    private static final String URL_SUBSPACE_RADIO_REWIND_PLAYER_PREFIX = "http://subspace-radio.net/modules/mod_rewind/player.php?recidx=";
    private static final String URL_EVE_RADIO_REWIND_PLAYER_PREFIX = "http://eve-radio.com/modules/mod_rewind/player.php?recidx=";

    private static final String WINDOWS_DUMMY_FILES = "createDumbyFiles.bat";
    private static final String LINUX_DUMMY_FILES = "./createDumbyFiles.sh";

    private static final String MSG_SEPARATOR = "####################################################################################################";
    private static final String MSG_PRO_TITLE = "###                           Downloading All Available DJ Rewind Shows                          ###";


    public static void main(final String args[]) {
        System.out.println(MSG_SEPARATOR);
        System.out.println(MSG_PRO_TITLE);
        System.out.println("#");

        final CLICommand command = new CLICommand();
        final RegexMatcher regexMatcher = new RegexMatcher();

        System.out.println("# Setting Up:");
        System.out.print("#  Checking Download Folder Exists: ");
        final File downloadDirectory = new File("EveRadioDownloads");
        if (downloadDirectory.exists()) {
            System.out.println("True");
        } else {
            System.out.println("False");
            System.out.println("#  Creating Download Folder");
            downloadDirectory.mkdir();
        }

        // command.runQuiet(WINDOWS_DUMMY_FILES);
        // command.runQuiet(LINUX_DUMMY_FILES);

        processRadioStation(command, regexMatcher, "SubspaceRadio_", URL_SUBSPACE_RADIO,
                URL_SUBSPACE_RADIO_REWIND_PLAYER_PREFIX, "Subspace", downloadDirectory);

        processRadioStation(command, regexMatcher, "EveRadio_", URL_EVE_RADIO, URL_EVE_RADIO_REWIND_PLAYER_PREFIX,
                "EVE", downloadDirectory);

    }


    private static void processRadioStation(final CLICommand command, final RegexMatcher regexMatcher,
            final String filePrefix, final String rewindPageURL, final String rewindPlayerURLPrefix,
            final String radioStationName, final File downloadDirectory) {

        System.out.println("#");
        System.out.println("# " + radioStationName + " Radio Section:");
        System.out.println("#  Downloading Rewind Page");
        final String radioRewindPage = command.run(PROGRAM_CURL + CONFIG_CURL + rewindPageURL);

        final List<RadioShow> radioShows = new ArrayList<RadioShow>();

        System.out.println("#  Processing Rewind Page: Show Rewind URLs");
        final List<String> rewindLinks = regexMatcher
                .getAllMatchning(RegexMatcher.PATTERN_REWIND_PAGE, radioRewindPage);

        System.out.println("#  Processing Rewind Page: Show Names");
        List<String> showNames = regexMatcher.getAllMatchning(RegexMatcher.PATTERN_SHOW_NAMES, radioRewindPage);
        showNames = regexMatcher.replaceAll("<div id='dj'>", "", showNames);
        showNames = regexMatcher.replaceAll(" (c", "_[C", showNames);
        showNames = regexMatcher.replaceAll(")", "]", showNames);
        showNames = regexMatcher.replaceAll("</div><div>", "_", showNames);
        showNames = regexMatcher.replaceAll("</div>", "", showNames);
        showNames = regexMatcher.replaceAll(" ", "_", showNames);
        showNames = regexMatcher.replaceAll(":", "", showNames);
        showNames = regexMatcher.reorderAllShowNames(showNames);
        System.out.println("#");

        int index = 0;
        for (final String rewindLink : rewindLinks) {
            final RadioShow radioShow = new RadioShow();
            radioShow.setShowRewindPlayerURL(rewindPlayerURLPrefix + rewindLink);
            radioShow.setShowFileName(showNames.get(index));
            radioShows.add(radioShow);
            index++;
        }

        System.out.println("#  Downloading Individual Rewind Pages");
        System.out.println("#  Processing Individual Rewind Pages: Show Playlists");
        for (final RadioShow radioShow : radioShows) {
            final String radioShowPlayListPage = command.run(PROGRAM_CURL + CONFIG_CURL
                    + radioShow.getShowRewindPlayerURL());
            radioShow.setShowRewindPlaylistURL(regexMatcher.getAllMatchning(RegexMatcher.PATTERN_PLAYLIST,
                    radioShowPlayListPage).get(0));
        }
        System.out.println("#");

        System.out.println("#  Downloading Individual Rewind Playlists");
        System.out.println("#  Processing Individual Rewind Playlists: Show MP3 URLs");
        for (final RadioShow radioShow : radioShows) {
            final String radioShowPlaylistFile = command.run(PROGRAM_CURL + CONFIG_CURL
                    + radioShow.getShowRewindPlaylistURL());
            final List<String> mp3UrlResults = regexMatcher.getAllMatchning(RegexMatcher.PATTERN_MP3,
                    radioShowPlaylistFile);
            radioShow.setShowRewindMP3URL(mp3UrlResults.get(mp3UrlResults.size() - 1));
        }
        System.out.println("#");

        index = 1;
        System.out.println("#  Downloading Each Rewind Show");
        for (final RadioShow radioShow : radioShows) {
            System.out.print("#   Downloading Show " + sizeToString(index) + " of " + sizeToString(radioShows.size())
                    + " : " + radioShow.getShowFileName() + " - ");
            final File radioShowMP3 = new File(downloadDirectory, filePrefix + radioShow.getShowFileName());
            if (radioShowMP3.exists()) {
                System.out.println("[Skipping]");
            } else {
                command.run(PROGRAM_CURL + CONFIG_CURL_DEBUG + CONFIG_CURL + radioShow.getShowRewindMP3URL() + " -o "
                        + downloadDirectory.getName() + File.separator + filePrefix + radioShow.getShowFileName());
                System.out.println("[Done]");
            }
            index++;
        }
        System.out.println("#");
        System.out.println(MSG_SEPARATOR);
    }


    private static String sizeToString(final Integer size) {
        return (size < 10) ? "0" + size : size.toString();
    }
}